package com.longrise.filedownloaderdemo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longrise.filedownloader.DownloadManager;
import com.longrise.filedownloader.DownloadService;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by xkj on 2016/3/11.
 * 添加缓存的页面
 */
public class AddDownloadLFView extends Activity implements ExpandableListView.OnChildClickListener, View.OnClickListener {

    private static final String TAG = "AddDownloadLFView";
    private Context mContext;
    private ExpandableListView mElv;
    private TextView mTvConfirm;
    private TextView mTvCheckAll;
    private RelativeLayout mRldetail;
    private TextView mTvNum;
    private ImageView mIvright;
    private RelativeLayout mRldef;  //设置清晰度
    private TextView mTvdef;
    private ImageView mIvdef;
    private RelativeLayout mRltitle;
    private PopupWindow mPWindow;
    private RelativeLayout mRlback;
    private String mExpiretime;
    private LinearLayout mRlNum;
    private List<String> mUrlList;
    private List<List<Downloaditembean>> mParentList;
    private AddDownLoadAdapter mAdapter;
    private List<Downloaditembean> mCheckedChildList = new ArrayList<>();
    private DownloadManager downloadManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lfview_adddownload);
        mContext = AddDownloadLFView.this;
        init();
    }


    public void init() {
        initView();
        initData();
        regEvent(true);
    }


    public void initView() {
        mRlback = (RelativeLayout) findViewById(R.id.adddownload_relapp_back);
        mRltitle = (RelativeLayout) findViewById(R.id.adddownload_title);
        mElv = (ExpandableListView) findViewById(R.id.adddownload_elv);
        mTvConfirm = (TextView) findViewById(R.id.adddownload_start_cache);
        mTvCheckAll = (TextView) findViewById(R.id.adddownload_check_all);
        mRldetail = (RelativeLayout) findViewById(R.id.adddownload_app_btrl);
        mTvNum = (TextView) findViewById(R.id.adddownload_tv_num);
        mRlNum = (LinearLayout) findViewById(R.id.adddownload_rl_num);
        mIvright = (ImageView) findViewById(R.id.adddownload_app_spinner);
        mRldef = (RelativeLayout) findViewById(R.id.adddownload_rl_definition);
        mTvdef = (TextView) findViewById(R.id.adddownload_tv_definition);
        mIvdef = (ImageView) findViewById(R.id.adddownload_iv_definition);
    }

    public void initData() {
        mCheckedChildList.clear();
        this.downloadManager = DownloadService.getDownloadManager();
        //this.downloadManager = DownloadManager.getInstance(mContext,"hehe");
        mAdapter =  new AddDownLoadAdapter(mContext, this.downloadManager);
        mElv.setAdapter(mAdapter);
        mElv.setGroupIndicator(null);
        mUrlList = new ArrayList<>();
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d17/d7/d7/f4-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d17/d27/d9/f1-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d10/d4/d1/f3-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d10/d4/d3/f1-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d20/d14/f2-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d13/d11/d1/f6-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d13/d11/d1/f10-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d13/d11/d1/f2-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d13/d11/d1/f4-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d13/d11/d1/f5-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d13/d11/d1/f9-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d13/d11/d1/f8-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d13/d11/d1/f7-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d13/d11/d1/f12-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d13/d11/d1/f1-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d13/d11/d1/f3-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d13/d11/d1/f11-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d15/d10/d1/f1-web.mp4");
        mUrlList.add("http://download.yxybb.com/bbvideo/web/d1/d12/d2/d1/f1-web.mp4");
        mParentList = new ArrayList<>();
        for (int i = 0; i < 1; i++) {
            List<Downloaditembean> childList = new ArrayList<>();
            for (int j = 0; j < mUrlList.size(); j++) {
                Downloaditembean bean = new Downloaditembean();
                bean.setName(j+".mp4");
                bean.setUrl(mUrlList.get(j));
                childList.add(bean);
            }
            mParentList.add(childList);
        }
        toCheckIfHasDownloaded(mParentList);
        mAdapter.setData(mParentList);
    }

    public void regEvent(boolean b) {
        if (mElv != null) {
            mElv.setOnChildClickListener(b ? this : null);
        }
        if (mTvConfirm != null) {
            mTvConfirm.setOnClickListener(b ? this : null);
        }
        if (mTvCheckAll != null) {
            mTvCheckAll.setOnClickListener(b ? this : null);
        }
        if (mRldetail != null) {
            mRldetail.setOnClickListener(b ? this : null);
        }
        if (mRldef != null) {
            mRldef.setOnClickListener(b ? this : null);
        }
        if (mRlback != null) {
            mRlback.setOnClickListener(b ? this : null);
        }

    }

    private void toCheckIfHasDownloaded(List<List<Downloaditembean>> parentList){
        for(int i=0;i<parentList.size();i++){
            for(int j=0;j<parentList.get(i).size();j++){
                Downloaditembean downloaditembean = parentList.get(i).get(j);
                String url = downloaditembean.getUrl();
                boolean addedTask = downloadManager.isHasAddedTask(url);
                if(addedTask){
                    downloaditembean.setDownloaded(true);
                }
            }
        }
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        Downloaditembean downloaditembean = mParentList.get(groupPosition).get(childPosition);
        boolean downloaded = downloaditembean.isDownloaded();
        if(!downloaded){
            if(downloaditembean.isChecked()){
                downloaditembean.setChecked(false);
                mCheckedChildList.remove(downloaditembean);
            }else{
                downloaditembean.setChecked(true);
                mCheckedChildList.add(downloaditembean);
            }
            mAdapter.notifyDataSetChanged();
        }else{
            Toast.makeText(mContext,"该视频已经加入缓存",Toast.LENGTH_SHORT).show();
        }

        //mAdapter.setData(mParentList);

        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.adddownload_start_cache: //确定缓存
                toCahce();
                break;
            case R.id.adddownload_check_all: //全选

                break;
            case R.id.adddownload_app_btrl: //进入下载管理页面

                break;
            case R.id.adddownload_rl_definition:  //切换清晰度

                break;

            case R.id.adddownload_relapp_back: //返回
                finish();
                break;

        }
    }

    public void toCahce(){

        for (int i=0;i<mCheckedChildList.size();i++){
            Downloaditembean downloaditembean = mCheckedChildList.get(i);
            String name = downloaditembean.getName();
            String url = downloaditembean.getUrl();
            String cachePath = FolderConstants.VIDEO_DIR+name;
            PrintLog.d(TAG,"cachePath:"+cachePath);
            PrintLog.d(TAG,"url:"+url);
            boolean result = downloadManager.addTask(url, cachePath);
            Log.d(TAG,"result:"+result);
            if(result){
                downloaditembean.setDownloaded(true);
                mAdapter.notifyDataSetChanged();
            }
        }
    }



}
